// pages/cart/cart.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        arr1:[],
        goodsList:[],
          totalData:0,
          goodsListCar:[
            {
              id:"1",
              goodsImage:"../../image/goods01.jpg",
              goodsName:"1商品名称商品名称商品名称",
              goodsAddress:"广州",
              goodsPrice:"200"
            },
            {
              id:"1",
              goodsImage:"../../image/goods01.jpg",
              goodsName:"2商品名称商品名称商品名称",
              goodsAddress:"广州",
              goodsPrice:"200"
            },
            {
              id:"1",
              goodsImage:"../../image/goods01.jpg",
              goodsName:"3商品名称商品名称商品名称",
              goodsAddress:"广州",
              goodsPrice:"200"
            },
          ]
    },
    getGoodsData(){
        // 请求数据
        let _this = this;
        wx.request({
          url: 'https://api-hmugo-web.itheima.net/api/public/v1/goods/search', 
          header: {
            'content-type': 'application/json' // 默认值
          },
          success (res) {
            let goodslist1 = res.data.message.goods;
            let lists = [];
            let imglists = [];
            goodslist1.forEach(item=>{
              let {goods_id,goods_name,goods_small_logo,goods_price,goods_big_logo} = item;
              let data = {
                  id:goods_id,
                  goodsImage:goods_small_logo,
                  goodsImagelist:goods_big_logo,
                  goodsName:goods_name,
                  goodsAddress:"广州",
                  goodsPrice:goods_price
              };
              if(goods_small_logo){
                lists.push(data);
              };
              if(goods_big_logo){
                imglists.push(data);
              }
            })
    
            // 修改数据
            _this.setData({
            goodsListCar:lists,
              imgs:imglists.slice(0,5)
            })
          }
        })
      },
    gopay(){
        wx.navigateTo({
          url: '/pages/pay/pay',
        })
    },
    // 加法计算
    add(options){
        console.log("加号",options)
        console.log(this);
        let index = options.target.dataset.index;
        // 获取this中data数据
        let num = this.data.goodsList[index].num + 1; //数值
        let key = "goodsList["+index+"].num";
        let obj = {};
        obj[key] = num;

        this.setData(obj);
        
        this.getTotal(); //计算总价格
    },
    // 减法计算
    reduce(options){
        let index = options.target.dataset.index;
        // 获取this中data数据
        let num = this.data.goodsList[index].num ; //数值
        let key = "goodsList["+index+"].num";
        let obj = {};
        if(num <= 1){
            obj[key] = 1;
        }else{
            num--; //数量减一
            obj[key] = num;
        }
        this.setData(obj);
        this.getTotal(); //计算总价格

    },

    // 删除
    del(options){
        // console.log(options);
        let index = options.currentTarget.dataset.index;
        this.data.goodsList.splice(index,1);
        // 更新数据同时更新视图
        this.setData({
            goodsList:this.data.goodsList
        })
        this.getTotal(); //计算总价格

    },

    // 计算总价格
    getTotal(){
        // 计算总价格
        let goodsList = this.data.goodsList;

        if(goodsList.length){
            let data = goodsList.reduce((total,item)=>{
                total = total + item.num * item.goodsPrice;
                return total;
            },0)
    
            
            this.setData({
                totalData:data
            })
        }
        

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.getGoodsData();
        // 获取数据
        var getCatData = wx.getStorageSync('GoodsCarList1');
        console.log(getCatData,"监听页面显示")
        var arr = [];
        var bbb = []
        getCatData.forEach(item => {
        //   console.log(item);
          if(item.num == 0){
              arr.push(item);
          }else if(item.statu == 0){
              bbb.push(item);
          }
        });
        this.setData({
          goodsList:bbb,
          arr1:arr
        })


        
        this.getTotal(); //计算总价格
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        console.log(this.data.goodsList,"bbbb")
        console.log(this.data.arr1,"cccc")
        let ccc = this.data.goodsList.concat(this.data.arr1)
        console.log(ccc,"safaf")
        // 页面隐藏修改存储数据
        if(ccc.length != 0){
          wx.setStorageSync('GoodsCarList1', ccc);
        }

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})